//
//  OptionBtn.m
//  TestOrderView
//
//  Created by 戈王 on 15/3/2.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "OptionBtn.h"
#import "UIView+Extension.h"

@implementation OptionBtn

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.height = 44;
        
        // 内部图标居中
        //        self.imageView.contentMode = UIViewContentModeCenter;
        [self setImage:[UIImage imageNamed:@"OS_orderTracking_radioBox_checkedBg"] forState:UIControlStateSelected];
        [self setImage:[UIImage imageNamed:@"OS_orderTracking_radioBox_normalBg"] forState:UIControlStateNormal];
        
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        // 高亮的时候不需要调整内部的图片为灰色
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}
/**
 *  设置内部图标的frame
 */
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(0, 0, 44, 44);
}

/**
 *  设置内部文字的frame
 */
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = 0;
    CGFloat titleX = 44;
    CGFloat titleH = 44;
    CGFloat titleW = self.width - 44;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    self.width = 44 + [self sizeWithfont:self.titleLabel.font maxSize:CGSizeMake(CGFLOAT_MAX, 44) andString:title].width + 5;
    
}

- (CGSize)sizeWithfont:(UIFont *)font maxSize:(CGSize)maxSize andString:(NSString *)string
{
    NSDictionary *dict = @{NSFontAttributeName : font};
    CGSize size =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}


@end
